﻿using System.IO;
using System.Xml;
using UnityEngine;

public class ConnectConfig{
    //腾讯云测试IP：140.143.129.237 局域网测试IP：172.27.35.2 本地127.0.0.1
    private static string _main_server_ip = null;
    private static int _main_server_port = -1;
    private static string _http_server_ip = null;
    private static int _http_server_port = -1;
    private static string _data_server_ip = null;
    private static int _data_server_port = -1;

    public static string main_server_ip {get { return _main_server_ip; } }
    public static string http_server_ip {get { return _http_server_ip; } }
    public static string data_server_ip {get { return _data_server_ip; } }

    public static int main_server_port { get { return _main_server_port; } }
    public static int http_server_port { get { return _http_server_port; } }
    public static int data_server_port { get { return _data_server_port; } }

    public static bool GetConnectConfig(){
        string path = Application.dataPath + "/LFramework/NetFramework/03.Config/ConnectConfig.xml";
        if (File.Exists(path))
        {
            //创建xml文档
            XmlDocument xml = new XmlDocument();
            xml.Load(path);

            //得到objects节点下的所有子节点
            XmlNodeList xmlNodeList = xml.SelectSingleNode("ConnectConfig").ChildNodes;
            
            //遍历所有子节点
            foreach (XmlElement child in xmlNodeList)
            {   
                switch (child.LocalName) {
                    case "main_server":
                        _main_server_ip = child.GetAttribute("ip");
                        _main_server_port = int.Parse(child.GetAttribute("port"));
                        break;
                    case "http_server":
                        _http_server_ip = child.GetAttribute("ip");
                        _http_server_port = int.Parse(child.GetAttribute("port"));
                        break;
                    case "data_server":
                        _data_server_ip = child.GetAttribute("ip");
                        _data_server_port = int.Parse(child.GetAttribute("port"));
                        break;
                    default:
                        break;
                }
            }
            return true;
        }
        else
        {
            return false;

        }
    }

}
    